#!/bin/bash
#2018年7月29日15:54:58
#auto ping check IP
#by author www.jfedu.net
########################
INPUT="0"
IP_LIST="$*"
RES_FILE1="/tmp/available.txt"
RES_FILE2="/tmp/unavailable.txt"
#Define check function 2020
check_lan(){
	read -p "Please enter the LAN segment,example 192.168.1.0 (Netmask/24): " INPUT
        if [ `echo $INPUT|sed 's/ /\n/g'|wc -l` -ne 0 ];then
                for IP in $(seq 1 254) 
                do
			IP_PREFIX=$(echo $INPUT|awk -F\. '{print $1"."$2"."$3"."}')
                        ping -c 2 -W1 ${IP_PREFIX}$IP >/dev/null 2>1
                        if [ $? -eq 0 ];then
                                echo "${IP_PREFIX}$IP is up."
                                echo "${IP_PREFIX}$IP" >> $RES_FILE1
                        else 
                                echo "${IP_PREFIX}$IP is down."
                                echo "${IP_PREFIX}$IP" >> $RES_FILE2
                        fi
                done
                echo -e "\033[32m-------------------------\033[0m"
                echo -e "\033[32mPlease check the following files:\033[0m"
                echo "Available IP addresses: $RES_FILE1"
                echo "Unavailable IP addresses: $RES_FILE2"
                echo 
        fi
}

check_list()
{
	read -p "Please enter the IP list to be checked,example list.txt: " INPUT
	if [ ! -z $INPUT ];then
		for IP in $(cat $INPUT) 
		do
	        	ping -c 2 -W1 $IP >/dev/null 2>1
	        	if [ $? -eq 0 ];then
	        		echo "$IP is up."
	        	    	echo $IP >> $RES_FILE1
	        	else 
	        		echo "$IP is down."
	        	    	echo $IP >> $RES_FILE2
	        	fi
		done
		echo -e "\033[32m-------------------------\033[0m"
		echo -e "\033[32mPlease check the following files:\033[0m"
		echo "Available IP addresses: $RES_FILE1"
		echo "Unavailable IP addresses: $RES_FILE2"
		echo 
	fi
}

check_ip(){
read -p "Please enter the IP to be checked,example 1.1.1.1 | 1.1.1.2: " INPUT
for INPUT in `echo $INPUT`
do
	while true
	do
		echo $INPUT|grep -E "\<([0-9]{1,3}\.){3}[0-9]{1,3}\>"	
		if [ $? -eq 0 ];then
			IP=(`echo $INPUT|sed 's/\./ /g'`)
			IP1=`echo ${IP[0]}`
			IP2=`echo ${IP[1]}`
			IP3=`echo ${IP[2]}`
			IP4=`echo ${IP[3]}`
			if [ $IP1 -gt 0 -a $IP1 -le 255 -a $IP2 -ge 0 -a $IP2 -le 255 -a $IP3 -ge 0 -a $IP3 -le 255 -a $IP4 -ge 0 -a $IP4 -lt 255 ];then
       				if [ `echo $INPUT|sed 's/ /\n/g'|wc -l` -ne 0 ];then
       				        for IP in $(echo $INPUT)
       				        do
       				                ping -c 2 -W1 $IP >/dev/null 2>1
       				                if [ $? -eq 0 ];then
       				                        echo "$IP is up."
       				                        echo $IP >> $RES_FILE1
       				                else
       				                        echo "$IP is down."
       				                        echo $IP >> $RES_FILE2
       				                fi
       				        done
       				        echo -e "\033[32m-------------------------\033[0m"
       				        echo -e "\033[32mPlease check the following files:\033[0m"
       				        echo "Available IP addresses: $RES_FILE1"
       				        echo "Unavailable IP addresses: $RES_FILE2"
       				        echo 
       				fi
			break;	
			else
				read -p "Please Enter server IP address:" INPUT
			fi
		else
			read -p "Please Enter server IP address:" INPUT
		fi
	done
done
}

case $1 in 
	1)
	check_lan
	;;
	2)
	check_ip
	;;
	3)
	check_list
	;;
	*)
	echo "---------------------------------------------"
	echo -e "\033[34mWelcome to LAN live scripts:\033[0m"
	echo -e "\033[32m1) check_lan\033[0m"
	echo -e "\033[32m2) check_ip\033[0m"
	echo -e "\033[32m3) check_list\033[0m"
	echo -e "\033[32mUsage:{/bin/sh $0 1|2|3|4|5|help}\033[0m"
	echo "---------------------------------------------"
esac
